package Code;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class Code94 {
    static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(){}
        TreeNode(int val){
            this.val = val;
        }
        TreeNode(int val,TreeNode left, TreeNode right){
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    static List<Integer> inorderTraversal(TreeNode root){
        List<Integer> res= new ArrayList<>();
//        inorder(root, res);
        Stack<TreeNode> stack = new Stack<>();
        TreeNode p = root;
        while(p != null || !stack.isEmpty()){
            while(p != null){
                stack.push(p);
                p  = p.left;
            }
            p = stack.pop();
            res.add(p.val);
            p = p.right;
        }

        return res;
    }

    static void inorder(TreeNode root, List<Integer> res) {
        if(root == null) return;
        inorder(root.left, res);
        res.add(root.val);
        inorder(root.right,res);
    }

    public static void main(String[] args) {
        // 构建示例二叉树
        TreeNode root = new TreeNode(1);
        root.right = new TreeNode(2);
        root.right.left = new TreeNode(3);

        List<Integer> traversalResult = inorderTraversal(root);
        System.out.println(traversalResult);
    }
}
